<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends CI_Controller {

 	public function __construct()
    {
        parent::__construct();
 
        $this->load->model('Facebook_model');
    }

	public function login()
	{
 		if($this->ion_auth->logged_in())
		{
			redirect('game');
		}

		$this->lang->load('public');

		$fb_data = $this->session->userdata('fb_data'); // This array contains all the user FB information

		$data = array('fb_data' => $fb_data);

		if(isset($fb_data['me']['name'])) {
			//$this->ion_auth->login();
		}
		
		$data['title'] = 'Login';

		$this->form_validation->set_rules('email', 'Email', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required');

		if ($this->form_validation->run() == FALSE) {
			$data['errors'] = validation_errors();
		} else {
			$identity = $this->input->post('email');
			$password = $this->input->post('password');
			if($this->ion_auth->login($identity, $password)) {
				if($this->ion_auth->is_admin()) {
					redirect('admin');
				} else {
					redirect('game');
				}
			}
		}

		$this->template->write_view('header', 'public/header', $data, TRUE);
		$this->template->write_view('nav', 'public/publicNav', $data, TRUE);
		$this->template->write_view('content', 'public/login', $data, TRUE);
		$this->template->write_view('footer', 'public/footer', $data, TRUE);
		$this->template->render();
	}

	public function logout()
	{
		if($this->ion_auth->logout()) {
			//$this->facebook->destroySession();
			redirect('auth/login');
		}
	}

	public function register()
	{
		if($this->ion_auth->logged_in())
		{
			redirect('game');
		}

		$this->lang->load('public');

		$data['title'] = 'Register Your FREE Account';

		$this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique[users.username]|alpha_dash|min_length[4]');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
		$this->form_validation->set_rules('emailconf', 'Email Confirmation', 'trim|required|matches[email]');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
		$this->form_validation->set_rules('passwordconf', 'Password Confirmation', 'trim|required|matches[password]');
		$this->form_validation->set_rules('planetname', 'Planet Name', 'trim|required|alpha_dash|min_length[4]');

		if ($this->form_validation->run() == FALSE) {
			$data['errors'] = validation_errors();
		} else {
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$email = $this->input->post('email');
			$additional_data = array('planet_name' => 'planetname');

			if($this->ion_auth->register($username, $password, $email, $additional_data))
			{
				$this->session->set_flashdata('success', 'value');
				redirect('auth/login');
			}
		}

		$this->template->write_view('header', 'public/header', $data, TRUE);
		$this->template->write_view('nav', 'public/publicNav', $data, TRUE);
		$this->template->write_view('content', 'public/register', $data, TRUE);
		$this->template->write_view('footer', 'public/footer', $data, TRUE);
		$this->template->render();
	}
}